package com.java110.mall.goods.cmd.product;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.product.ProductDto;
import com.java110.mall.goods.bmo.product.IGetProductBMO;
import com.java110.mall.goods.bmo.productSpec.IGetProductSpecBMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;

@Java110Cmd(serviceCode = "product.queryProductAndSpec")
public class QueryProductAndSpecCmd extends Cmd {

    @Autowired
    private IGetProductBMO getProductBMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        super.validatePageInfo(reqJson);
        Assert.hasKeyAndValue(reqJson, "shopId", "未包含店铺");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        ProductDto productDto = BeanConvertUtil.covertBean(reqJson, ProductDto.class);
        String createTime = reqJson.getString("createTime");
        if (!StringUtil.isEmpty(createTime)) {
            productDto.setCreateTime(DateUtil.getDateFromStringB(createTime));
        }
        ResponseEntity responseEntity = getProductBMOImpl.queryProductAndSpec(productDto);
        context.setResponseEntity(responseEntity);
    }
}
